﻿using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Controls
{
	public class Panel
		: ContentControl
	{
		private Border _border;

		public Color BorderColor { get; set; }
		public Color BackgroundColor { get; set; }

		public override void Deserialize(XElement element, object callingObject)
		{
			base.Deserialize(element, callingObject);

			BorderColor = element.GetColor("BorderColor", Color.White);
			BackgroundColor = element.GetColor("BackgroundColor", Color.LightGray);
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);

			if (_border == null)
			{
				_border = new Border
				{
					BorderColor = BorderColor,
					Size = Size,
					Position = Position,
					Skin = Skin,
					Font = Font,
				};
			}

			for (var i = 0; i < Children.Count; i++)
			{
				var child = Children[i];
				child.Update(gameTime);
			}
		}

		public override void Draw(GameTime gameTime)
		{
			var whitePixel = RenderEngine.Get().Textures["WhitePixel"];

			var backgroundRect = new Rectangle((int)(Position.X + 2), (int)(Position.Y + 2), (int)(Size.X - 4), (int)(Size.Y - 4));
			SpriteBatch.Draw(whitePixel, backgroundRect, BackgroundColor);

			_border.Draw(gameTime);

			for (var i = 0; i < Children.Count; i++)
			{
				var child = Children[i];
				child.Draw(gameTime);
			}
		}
	}
}